package com.rc.saas.tenant.service.member;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.member.MemberCardMapper;
import com.rc.saas.tenant.mapper.member.MemberCardRecordMapper;
import com.rc.saas.tenant.model.member.MemberCard;
import com.rc.saas.tenant.model.member.MemberCardExample;
import com.rc.saas.tenant.model.member.MemberCardRecord;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.member.MemberCardSearch;
import com.rc.saas.tenant.vo.member.MemberCardResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2019/11/21
 */
@Service
public class MemberCardServiceImpl implements MemberCardService {

    @Autowired
    private MemberCardMapper memberCardMapper;

    @Autowired
    private MemberCardRecordMapper memberCardRecordMapper;

    @Override
    public MemberCardMapper getMapper() {
        return memberCardMapper;
    }

    @Override
    //@Transactional(rollbackFor = Exception.class)
    public void bindMobile(Long cardId, MemberInfo memberInfo, TenantUser user) {
        Date date = new Date();
        MemberCard card = memberCardMapper.selectByPrimaryKey(cardId);

        //创建记录对象
        MemberCardRecord memberCardRecord = new MemberCardRecord();
        memberCardRecord.setCardId(cardId);
        memberCardRecord.setTenantCode(user.getTenantCode());
        memberCardRecord.setCardNo(card.getCardNo());
        //设置默认值
        memberCardRecord.setCardType("0");
        memberCardRecord.setAccountType("0");
        memberCardRecord.setCreatedBy(user.getLoginName());
        memberCardRecord.setCreatedTime(date);
        memberCardRecord.setLastUpdatedBy(user.getLoginName());
        memberCardRecord.setLastUpdatedTime(date);

        //membreInfo是null 为解绑操作
        if (memberInfo == null) {
            memberCardRecord.setMemberId(null);
            //记录类型:0 未绑定 1 已绑定  2已注销 默认0
            memberCardRecord.setRecordType("0");

            card.setMemberId(null);
            card.setOperateStatus("0");
            card.setChangeTime(null);
        } else {
            memberCardRecord.setMemberId(memberInfo.getMemberId());
            memberCardRecord.setRecordType("1");

            card.setMemberId(memberInfo.getMemberId());
            //状态:0 未绑定 1 已绑定  2已注销 默认0
            card.setOperateStatus("1");
            card.setChangeTime(date);
        }
        card.setLastUpdatedTime(date);
        card.setLastUpdatedBy(user.getLoginName());

        memberCardRecordMapper.insertSelective(memberCardRecord);
        memberCardMapper.updateByPrimaryKey(card);
    }

    @Override
    //@Transactional(rollbackFor = Exception.class)
    public void cancelCard(TenantUser user, MemberCard card) {
        Date date = new Date();
        card.setLastUpdatedTime(date);
        card.setLastUpdatedBy(user.getLoginName());
        card.setMemberId(null);
        //状态:0 未绑定 1 已绑定  2已注销 默认0
        card.setOperateStatus("2");
        card.setChangeTime(null);
        memberCardMapper.updateByPrimaryKey(card);

        MemberCardRecord memberCardRecord = new MemberCardRecord();
        memberCardRecord.setTenantCode(user.getTenantCode());
        memberCardRecord.setCardId(card.getCardId());
        memberCardRecord.setCardNo(card.getCardNo());
        memberCardRecord.setCardType("0");
        memberCardRecord.setAccountType("0");
        memberCardRecord.setRecordType("2");
        memberCardRecord.setCreatedTime(date);
        memberCardRecord.setCreatedBy(user.getLoginName());
        memberCardRecord.setLastUpdatedTime(date);
        memberCardRecord.setLastUpdatedBy(user.getLoginName());
        memberCardRecordMapper.insertSelective(memberCardRecord);
    }

    @Override
    public PageInfo<MemberCardResult> searchByCondition(MemberCardSearch memberCardSearch) {
        PageHelper.startPage(memberCardSearch.getOffset(), memberCardSearch.getLimit());
        List<MemberCardResult> memberCardResults = memberCardMapper.searchByCondition(memberCardSearch);
        return new PageInfo<MemberCardResult>(memberCardResults);
    }

    @Override
    public MemberCard selectLastCardNo(String tenantCode) {
        return memberCardMapper.selectLastCardNo(tenantCode);
    }

    @Override
    public TResponse isBindMobile(Long memberId,String tenantCode) {
        MemberCardExample example = new MemberCardExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andTenantCodeEqualTo(tenantCode);
        List<MemberCard> memberCards = memberCardMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(memberCards)) {
            return TResponse.FAIL("该会员已经绑定过会员卡");
        }
        return null;
    }

}
